package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallOrderItemMapper;
import com.hmall.mapper.MallOrderMapper;
import com.hmall.mapper.MallPriceMapper;
import com.hmall.mapper.MallProductMapper;
import com.hmall.model.MallPrice;
import com.hmall.model.MallProduct;
import com.hmall.util.EnvUtil;
import com.hdmall.business.common.util.HttpClientUtil;
import com.hmall.vo.MallProductWithGradeVo;
import com.hmall.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderService
 * @Description
 * @Author zhoutao
 * @Date 2018/12/5 15:04
 * @Version 2.0
 **/
@Service
public class OrderService {


    @Autowired
    MallOrderMapper mallOrderMapper;

    @Autowired
    MallProductMapper mallProductMapper;

    @Autowired
    MallOrderItemMapper mallOrderItemMapper;

    @Autowired
    MallPriceMapper mallPriceMapper;
    /**
     * @Date 2018/12/5 15:10
     * @Param [userId]
     * @return com.hmall.common.ServerResponse
     * @Description  列出历史产品列表
     */
    public ServerResponse listHistoryProduct(Integer userId, Integer pageNum, Integer pageSize) {

        Integer total = mallOrderItemMapper.selectCountByUserId(userId);
        if(total == 0) {
            return ServerResponse.createBySuccess();
        }
        Integer start = pageSize * (pageNum - 1);
        Integer end = Math.min(pageSize, start + pageSize);
        List<MallPrice> priceList = mallPriceMapper.selectByCountWithLimit(userId, start, end);
        List<MallProductWithGradeVo> mallProductWithGradeVoList = new ArrayList<>();
        for(MallPrice mallPrice : priceList){

            String grade = mallPrice.getGrade();
            if(grade.contains("定重")){
                if(grade.contains("不")){
                    grade = "AA不定重";
                }else{
                    grade = "AA定重";
                }
            }
            MallProduct mallProduct = mallProductMapper.selectByProductId(mallPrice.getProductId());
            MallProductWithGradeVo mallProductWithGradeVo = new MallProductWithGradeVo(mallProduct.getId(), mallProduct.getCategoryId(), mallProduct.getName(), mallProduct.getSubtitle(),
                    mallProduct.getMainImage(), mallProduct.getPrice(), mallProduct.getStock(), mallProduct.getStatus(), mallProduct.getCreateTime(), mallProduct.getUpdateTime(), mallProduct.getProductno(), mallProduct.getShopid(),
                    mallProduct.getUprice(), mallProduct.getCommentNum(), mallProduct.getSpecial(), mallProduct.getSearchHot(), mallProduct.getIsnew(), mallProduct.getShopId(), mallProduct.getSubImages(), mallProduct.getDetail(),
                    mallProduct.getContent(), mallProduct.getTitle(), grade);
            mallProductWithGradeVoList.add(mallProductWithGradeVo);
        }


        PageVo pageVo = new PageVo(pageNum, pageSize, total, mallProductWithGradeVoList);
        return ServerResponse.createBySuccess(pageVo);

    }

    public ServerResponse createOrderHistory(String token, Integer customerUserId, String productPlanOrderJsonStr, Integer shippingId, Integer distCode, String remark, Integer transportId, String type) {

        Map map = new HashMap<>(16);
        map.put("token", token);
        map.put("customerUserId", customerUserId.toString());
        map.put("productPlanOrderJsonStr", productPlanOrderJsonStr);
        map.put("shippingId", shippingId.toString());
        map.put("distCode", distCode.toString());
        map.put("remark", remark);
        map.put("transportId", "123");
        map.put("type", "history");
        EnvUtil envUtil = new EnvUtil();
        String url = envUtil.getPreUrl("order");
        String s = HttpClientUtil.doPostWithParam(url + "/order/createOrderByPlan", map);



        return ServerResponse.createBySuccess(s);
    }
}
